/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.jbr.simplefoodplan.rest.impl;

import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Query;
import static com.jbr.simplefoodplan.SecuredDatastore.*;
import com.jbr.simplefoodplan.entities.Dish;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

/**
 *
 * @author jbr
 */
@Path("/dish/")
public class DishResource {
    private Dish makeADish() {
        final Dish dish = new Dish();
        dish.setName("Frikadeller");
        return dish;
    }

    @GET
    @Produces({"application/json", "application/xml"})
    @Path("/{dishId}")
    public Dish getDish(@PathParam ("dishId") Long dishId) {
        Dish dish;
        try {
            dish = get(Dish.class, dishId);
        } catch (EntityNotFoundException ex) {
            return null;
        }
        dish.setAddUsagesInPlans(true);
        return dish;
    }


    @DELETE
    @Consumes({"text/plain"})
    @Path("/{dishId}")
    public void removeDishById(@PathParam ("dishId") Long dishId) throws IOException {
        Dish dish = getDish(dishId);
        delete(dish);
    }

    @DELETE
    @Consumes({"application/json", "application/xml"})
    @Path("/{dishId}")
    public void removeDish(Dish dish) throws IOException {
        removeDishById(dish.getId());
    }

    @PUT
    @Produces({"application/json", "application/xml"})
    @Consumes({"application/json", "application/xml"})
    @Path("/{dishId}")
    public Dish editDish(Dish dish) {
        System.out.println("Dish name in dish is " + dish.getName());
        return put(dish);
    }

    @POST
    @Produces({"application/json", "application/xml"})
    @Consumes({"application/json", "application/xml"})
    @Path("/add")
    public Dish addDish(Dish dish) {
        //dish.setId(null);
        System.out.println(dish.getName());
        return put(dish);
    }

    @POST
    @Produces({"application/json", "application/xml"})
    @Consumes({"application/json", "application/xml"})
    @Path("/addlist")
    public void addDishList(List<Dish> dishes) {
        //dish.setId(null);
        for (Dish dish : dishes) {
            System.out.println(dish.getName());
            put(dish);
        }
    }

    @GET
    @Produces({"application/json", "application/xml"})
    @Path("/list")
    public List<Dish> getAllDishes() throws IOException {
        //final String query = "select from " + Dish.class.getName() + " order by name desc";
        Query query = new Query("dish");
        List<Dish> dishes = getList(Dish.class, query, 2000);
        System.out.println("Number of dishes in list=" + dishes.size());
        Collections.sort(dishes, new Comparator<Dish>() {

            @Override
            public int compare(Dish o1, Dish o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        for (Dish dish : dishes)
            dish.setAddUsagesInPlans(true);
        return dishes;
    }

    @GET
    @Produces("text/plain")
    @Path("/makeadish")
    public String doMakeADish() {
        Dish newdish = put(makeADish());
        //Greeting greeting = new Greeting(null, "something", new Date());
        return "Created a new plan with id=" + newdish.getId();
    }
}
